﻿Imports System.Xml

Public Class ClsObservation

    Private m_WhatTaxonId As Nullable(Of Integer)
    Private m_WhatTaxonName As String
    Private m_Quality As String
    Private m_Photo As String
    Private m_ProtocolName As String
    Private m_Field1Name As String
    Private m_Field1Type As String
    Private m_Field1Unit As String
    Private m_Field1Value As String
    Private m_Field2Name As String
    Private m_Field2Type As String
    Private m_Field2Unit As String
    Private m_Field2Value As String
    

    '##########################################################################################################################
    '                       FONCTIONS
    '##########################################################################################################################
    '""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    'Fonction qui tranforme l'object Observation en XML
    '""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    Public Function ExportObsToXML(ByRef writer As XmlWriter) As XmlWriter
        writer.WriteStartElement("WHAT")
        writer.WriteStartElement("TAXON")
        writer.WriteElementString("TAXON_ID", Me.m_WhatTaxonId.ToString)
        writer.WriteElementString("TAXON_NAME", Me.m_WhatTaxonName)
        writer.WriteElementString("QUALITY", Me.m_Quality)
        writer.WriteElementString("PHOTO", Me.m_Photo)
        writer.WriteEndElement()
        writer.WriteStartElement("OBSERVATIONS")
        writer.WriteAttributeString("Protocol_Name", Me.m_ProtocolName)
        writer.WriteStartElement("OBSERVATION")
        writer.WriteAttributeString("Id", "1")
        writer.WriteElementString("NAME", Me.m_Field1Name)
        writer.WriteElementString("TYPE", Me.m_Field1Type)
        writer.WriteElementString("UNIT", Me.m_Field1Unit)
        writer.WriteElementString("VALUE", Me.m_Field1Value)
        writer.WriteEndElement()
        writer.WriteStartElement("OBSERVATION")
        writer.WriteAttributeString("Id", "2")
        writer.WriteElementString("NAME", Me.m_Field2Name)
        writer.WriteElementString("TYPE", Me.m_Field2Type)
        writer.WriteElementString("UNIT", Me.m_Field2Unit)
        writer.WriteElementString("VALUE", Me.m_Field2Value)
        writer.WriteEndElement()
        writer.WriteEndElement()
        writer.WriteEndElement()
        Return writer
    End Function



    '##########################################################################################################################
    '                       CONSTRUCTEURS et Cie
    '##########################################################################################################################
    Public Sub New()

    End Sub

    Public Sub New(ByVal itaxonId As Nullable(Of Integer), ByVal sTaxonName As String, ByVal Quality As String, ByVal Photo As String, ByVal ProtocolName As String, _
                        ByVal Field1Name As String, ByVal Field1Type As String, ByVal Field1Unit As String, ByVal Field1Value As String, _
                        ByVal Field2Name As String, ByVal Field2Type As String, ByVal Field2Unit As String, ByVal Field2Value As String)

        Me.InitWhat(itaxonId, sTaxonName, Quality, Photo, ProtocolName, Field1Name, Field1Type, Field1Unit, Field1Value, Field2Name, Field2Type, Field2Unit, Field2Value)

    End Sub

    Public Sub New(ByVal sXmlObservation As String)
        Dim m_xmld As XmlDocument

        Try
            'Création du document XML
            m_xmld = New XmlDocument()
            'Chargement du fichier XML
            Dim m_sr As New System.IO.StringReader(sXmlObservation)
            m_xmld.Load(m_sr)

            'Récupération des données WHAT et instanciation des variables
            '<WHAT>
            '   <TAXON>
            '           <TAXON_ID/>
            '           <TAXON_NAME>potamo</TAXON_NAME>
            '           <QUALITY>Null</QUALITY>
            '           <PHOTO>Null</PHOTO>
            '   </TAXON>
            '   <OBSERVATIONS Protocol_Name="protocole">
            '       <OBSERVATION Id="1">
            '           <NAME>presque </NAME>
            '           <TYPE>entier</TYPE>
            '           <UNIT>m</UNIT> 
            '           <VALUE>110</VALUE>
            '       </OBSERVATION>
            '       <OBSERVATION Id="2">
            '           <NAME>Null</NAME>
            '           <TYPE>Null</TYPE>
            '           <UNIT>Null</UNIT>
            '           <VALUE>Null</VALUE>
            '       </OBSERVATION>
            '   </OBSERVATIONS>
            '</WHAT>
            Me.InitWhat(GetNodeTypeInteger(m_xmld, "/WHAT/TAXON/TAXON_ID"), m_xmld.SelectNodes("/WHAT/TAXON/TAXON_NAME").Item(0).InnerText, m_xmld.SelectNodes("/WHAT/TAXON/QUALITY").Item(0).InnerText, _
                        m_xmld.SelectNodes("/WHAT/TAXON/PHOTO").Item(0).InnerText, m_xmld.SelectNodes("/WHAT/OBSERVATIONS").Item(0).Value, m_xmld.SelectNodes("/WHAT/OBSERVATIONS/OBSERVATION [@Id= '1' ]/NAME").Item(0).InnerText, _
                        m_xmld.SelectNodes("/WHAT/OBSERVATIONS/OBSERVATION [@Id= '1' ]/TYPE").Item(0).InnerText, m_xmld.SelectNodes("/WHAT/OBSERVATIONS/OBSERVATION [@Id= '1' ]/UNIT").Item(0).InnerText, _
                        m_xmld.SelectNodes("/WHAT/OBSERVATIONS/OBSERVATION [@Id= '1' ]/VALUE").Item(0).InnerText, _
                         m_xmld.SelectNodes("/WHAT/OBSERVATIONS/OBSERVATION [@Id= '2' ]/NAME").Item(0).InnerText, m_xmld.SelectNodes("/WHAT/OBSERVATIONS/OBSERVATION [@Id= '2' ]/TYPE").Item(0).InnerText, _
                         m_xmld.SelectNodes("/WHAT/OBSERVATIONS/OBSERVATION [@Id= '2' ]/UNIT").Item(0).InnerText, _
                         m_xmld.SelectNodes("/WHAT/OBSERVATIONS/OBSERVATION [@Id= '2' ]/VALUE").Item(0).InnerText)

        Catch errorVariable As Exception
            'Error trapping
            Console.Write(errorVariable.ToString())
        End Try

    End Sub

    Public Sub InitWhat(ByVal itaxonId As Nullable(Of Integer), ByVal sTaxonName As String, ByVal Quality As String, ByVal Photo As String, ByVal ProtocolName As String, _
                        ByVal Field1Name As String, ByVal Field1Type As String, ByVal Field1Unit As String, ByVal Field1Value As String, _
                        ByVal Field2Name As String, ByVal Field2Type As String, ByVal Field2Unit As String, ByVal Field2Value As String)
        Me.m_WhatTaxonId = itaxonId
        Me.m_WhatTaxonName = sTaxonName
        Me.m_Quality = Quality
        Me.m_Photo = Photo
        Me.m_ProtocolName = ProtocolName
        Me.m_Field1Name = Field1Name
        Me.m_Field1Type = Field1Type
        Me.m_Field1Unit = Field1Unit
        Me.m_Field1Value = Field1Value
        Me.m_Field2Name = Field2Name
        Me.m_Field2Type = Field2Type
        Me.m_Field2Unit = Field2Unit
        Me.m_Field2Value = Field2Value
    End Sub


    '##########################################################################################################################
    '                       ACCESSEURS
    '##########################################################################################################################


    Public Property WhatTaxonId() As Nullable(Of Integer)
        Get
            Return m_WhatTaxonId
        End Get
        Set(ByVal value As Nullable(Of Integer))
            m_WhatTaxonId = value
        End Set
    End Property

    Public Property WhatTaxonName() As String
        Get
            Return m_WhatTaxonName
        End Get
        Set(ByVal value As String)
            m_WhatTaxonName = value
        End Set
    End Property

    
    Public Property Quality() As String
        Get
            Return m_Quality
        End Get
        Set(ByVal value As String)
            m_Quality = value
        End Set
    End Property

    Public Property Photo() As String
        Get
            Return m_Photo
        End Get
        Set(ByVal value As String)
            m_Photo = value
        End Set
    End Property


    Public Property ProtocolName() As String
        Get
            Return m_ProtocolName
        End Get
        Set(ByVal value As String)
            m_ProtocolName = value
        End Set
    End Property

    Public Property Field1Name() As String
        Get
            Return m_Field1Name
        End Get
        Set(ByVal value As String)
            m_Field1Name = value
        End Set
    End Property

    Public Property Field1Type() As String
        Get
            Return m_Field1Type
        End Get
        Set(ByVal value As String)
            m_Field1Type = value
        End Set
    End Property
    Public Property Field1Unit() As String
        Get
            Return m_Field1Unit
        End Get
        Set(ByVal value As String)
            m_Field1Unit = value
        End Set
    End Property

    Public Property Field1Value() As String
        Get
            Return m_Field1Value
        End Get
        Set(ByVal value As String)
            m_Field1Value = value
        End Set
    End Property
    Public Property Field2Name() As String
        Get
            Return m_Field2Name
        End Get
        Set(ByVal value As String)
            m_Field2Name = value
        End Set
    End Property

    Public Property Field2Type() As String
        Get
            Return m_Field2Type
        End Get
        Set(ByVal value As String)
            m_Field2Type = value
        End Set
    End Property
    Public Property Field2Unit() As String
        Get
            Return m_Field2Unit
        End Get
        Set(ByVal value As String)
            m_Field2Unit = value
        End Set
    End Property

    Public Property Field2Value() As String
        Get
            Return m_Field2Value
        End Get
        Set(ByVal value As String)
            m_Field2Value = value
        End Set
    End Property

End Class
